<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/18
 * Time: 9:11
 */
namespace app\admin\controller;
use phpexcel\PHPExcel;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Jlou extends Common
{
    /*
     * 商品
     */
    public function goods()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_del'] = array('eq', 0);
            if (isset($pagedata['shop_name']) && $pagedata['shop_name'] != '') {
                $where['shop_name'] = array('like', '%' . $pagedata['shop_name'] . '%');
            }
            if (isset($pagedata['is_kaijiang']) && $pagedata['is_kaijiang'] != '') {
                $where['is_kaijiang'] =  $pagedata['is_kaijiang'];
            }

            $count = Db::name('jlou_goods')->where($where)->count();
            $data = Db::name('jlou_goods')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $res = Db::name('jlou_goods')->select();
            if ($res) {
                foreach ($res as $k => $v) {
                    if ($v['is_kaijiang'] == 0 && time() > strtotime($v['time'])) {
                        if ($v['number'] < $v['shop_min_num']) {
                            Db::name('jlou_goods')->where('id', $v['id'])->setField('is_kaijiang', 3);
                        } else {
                            Db::name('jlou_goods')->where('id', $v['id'])->setField('is_kaijiang', 1);
                        }
                    }
                }
            }

            return view();
        }
    }

    /*
     * 添加商品
     */
    public function addGoods()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            unset($post['file']);
            if (!$post['article_thumb']) {
                return ['code' => 1, 'msg' => '请上传商品缩略图'];
                exit;
            }

            if (!$post['content']) {
                return ['code' => 1, 'msg' => '商品详情不能为空'];
                exit;
            }
            if (!$post['article_thumb1']) {
                return ['code' => 1, 'msg' => '请上传捡漏缩略图'];
                exit;
            }

            if (!$post['t_content']) {
                return ['code' => 1, 'msg' => '捡漏详情不能为空'];
                exit;
            }
            //查询期数
            $da = Db::name('jlou_goods')->where('date',$post['date'])->find();
            if($da){
                return ['code' => 1, 'msg' => '该期数已经存在'];
                exit;
            }
            if($post['is_real']==1){
                $data = [
                    'date' => $post['date'],
                    'time' => $post['time'],
                    'shop_name' => $post['shop_name'],
                    'shop_price' => $post['shop_price'],
                    'shop_min_num' => $post['shop_min_num'],
                    'shop_max_num' => $post['shop_max_num'],
                    'shop_img' => $post['article_thumb'],
                    'content' => $post['content'],
                    't_shop_name' => $post['t_shop_name'],
                    't_shop_price' => $post['t_shop_price'],
                    't_shop_img' => $post['article_thumb1'],
                    't_content' => $post['t_content'],
                    'create_time' => date('Y-m-d H:i:s'),
                    'is_real'=>$post['is_real'],
                    'real_date'=>$post['coupon'],
                ];
            }else{
                $data = [
                    'date' => $post['date'],
                    'time' => $post['time'],
                    'shop_name' => $post['shop_name'],
                    'shop_price' => $post['shop_price'],
                    'shop_min_num' => $post['shop_min_num'],
                    'shop_max_num' => $post['shop_max_num'],
                    'shop_img' => $post['article_thumb'],
                    'content' => $post['content'],
                    't_shop_name' => $post['t_shop_name'],
                    't_shop_price' => $post['t_shop_price'],
                    't_shop_img' => $post['article_thumb1'],
                    't_content' => $post['t_content'],
                    'create_time' => date('Y-m-d H:i:s'),
                    'is_real'=>$post['is_real'],
                ];
            }

            $res = Db::name('jlou_goods')->insert($data);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        } else {

            return $this->fetch();
        }
    }




    /*
     * 上架商品
     */
    public function isShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
          //查看当前捡漏商品是否已经被用户购买
            $goodsIsBuy = Db::name('jlou_order')
            ->where(['goods_id'=>$post['id'],'order_status'=>1,'is_refund'=>0])
            ->select();
            if(count($goodsIsBuy)>0){
                return ['code' => 1 ,'msg'=>'已有用户支付,请勿轻易修改捡漏信息'];
            }
            $res = Db::name('jlou_goods')->where('id', $post['id'])->setField('is_display', $post['is_display']);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 删除图片
     */
    public function del()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('jlou_goods')->where('id', $post['id'])->setField('is_del', 1);
            if ($res) {
                return '1';
            } else {
                return '2';
            }
        }
    }

    /*
     * 商品详情
     */
    public function details()
    {
        $id = input('id', '', 'trim');
        $content = Db::name('jlou_goods')->field('content')->where('id', $id)->find();
        $this->assign('content', $content);
        return $this->fetch();
    }

    /*
     * 捡漏详情
     */
    public function good_details()
    {
        $id = input('id', '', 'trim');
        $content = Db::name('jlou_goods')->field('t_content as content')->where('id', $id)->find();
        $this->assign('content', $content);
        return $this->fetch();
    }

    /*
     * 修改
     */
    public function editGoods()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            unset($post['file']);
            if (!$post['article_thumb']) {
                return ['code' => 1, 'msg' => '请上传商品缩略图'];
                exit;
            }

            if (!$post['content']) {
                return ['code' => 1, 'msg' => '商品详情不能为空'];
                exit;
            }
            if (!$post['article_thumb1']) {
                return ['code' => 1, 'msg' => '请上传捡漏缩略图'];
                exit;
            }

            if (!$post['t_content']) {
                return ['code' => 1, 'msg' => '捡漏详情不能为空'];
                exit;
            }
              //查看当前捡漏商品是否已经被用户购买
            $goodsIsBuy = Db::name('jlou_order')
            ->where(['goods_id'=>$post['id'],'order_status'=>1,'is_refund'=>0])
            ->select();
            if(count($goodsIsBuy)>0){
                return ['code' => 1 ,'msg'=>'已有用户支付,请勿轻易修改捡漏信息'];
            }
            $data = [
                'date' => $post['date'],
                'time' => $post['time'],
                'shop_name' => $post['shop_name'],
                'shop_price' => $post['shop_price'],
                'shop_min_num' => $post['shop_min_num'],
                'shop_max_num' => $post['shop_max_num'],
                'shop_img' => $post['article_thumb'],
                'content' => $post['content'],
                't_shop_name' => $post['t_shop_name'],
                't_shop_price' => $post['t_shop_price'],
                't_shop_img' => $post['article_thumb1'],
                't_content' => $post['t_content'],
            ];
            $res = Db::name('jlou_goods')->where('id', $post['id'])->update($data);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        } else {
            $id = Request::instance()->get();
            $data = Db::name('jlou_goods')->where('id', $id['id'])->find();

            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /*
     * 开奖
     */
    public function kai()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            //获取该产品的所有参与的个数
            $count = Db::name('jlou_order')->where('goods_id', $post['data']['id'])->count();
            $show = $post['data']['content'] % $count + 10000000;
            //修改信息
            $data = [
                'code' => $show,
                'open_time' => date('Y-m-d H:i:s'),
                'is_kaijiang' => 2
            ];
            //发送短息和站内信息
            $date = Db::name('jlou_goods')->alias('a')->field('date,t_shop_name,u.phone as tel,b.uid as user_id')
                ->join('jlou_order b', 'b.goods_id=a.id', 'left')
                ->join('user u', 'b.uid=u.uid', 'left')
                ->where('b.order_code', $show)
                ->where('goods_id', $post['data']['id'])->find();


            $res = Db::name('jlou_goods')->where('id', $post['data']['id'])->update($data);

            if ($res) {
                //查询所有的人
                $user = Db::name('jlou_order')->where('goods_id', $post['data']['id'])->where('order_status', 1)->select();
                foreach ($user as $k => $v) {
                    if ($v['order_code'] == $show) {
                        Db::name('jlou_order')->where('goods_id', $post['data']['id'])->where('order_code', $show)->setField('is_have', 1);
                    }
                }
                $send = [
                    '1' => $date['date'],
                    '2' => $date['t_shop_name']
                ];
                sendJsmsMessage($date['tel'], 177265, $send);
                send_user_mes($date['user_id'], '恭喜您拔得头筹', "在捡漏" . $date['date'] . "期中获得" . $date['t_shop_name'] . "奖品");
                return ['code' => $show];
            } else {
                return ['code' => 0];
            }

        }
    }

    /*****************************************************捡漏订单****************************************/
    public function order()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['order_sn']) && $pagedata['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $pagedata['order_sn'] . '%');
            }
            if (isset($pagedata['date']) && $pagedata['date'] != '') {
                $where['date'] =  $pagedata['date'];
            }
            if (isset($pagedata['is_kaijiang']) && $pagedata['is_kaijiang'] != '') {
                $where['is_kaijiang'] = array('like', '%' . $pagedata['is_kaijiang'] . '%');
            }
            if (isset($pagedata['order_status']) && $pagedata['order_status'] != '') {
                $where['order_status'] = array('like', '%' . $pagedata['order_status'] . '%');
            }
            $count = Db::name('jlou_order')->alias('a')->field('a.*,b.date')->join('jlou_goods b', 'a.goods_id=b.id', 'left')
                ->where($where)->count();
            $data = Db::name('jlou_order')->alias('a')->field('a.*,b.date,b.is_kaijiang')->join('jlou_goods b', 'a.goods_id=b.id', 'left')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('order_id', 'desc')
                ->select();
            Session::set('jlou_data',$data);
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $res = Db::name('jlou_order')->field('order_sn')->select();
            $this->assign('res', $res);
            $date = Db::name('jlou_goods')->field('date')->select();
            $this->assign('date', $date);

            $res = Db::name('jlou_goods')->select();
            if ($res) {
                foreach ($res as $k => $v) {
                    if ($v['is_kaijiang'] == 0 && time() > strtotime($v['time'])) {
                        if ($v['number'] < $v['shop_min_num']) {
                            Db::name('jlou_goods')->where('id', $v['id'])->setField('is_kaijiang', 3);
                        } else {
                            Db::name('jlou_goods')->where('id', $v['id'])->setField('is_kaijiang', 1);
                        }
                    }
                }
            }
            return view();
        }
    }

    /**
     * 点击发货
     */
    public function send_code()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = [
                'dk_code' => $post['data']['content'],
                'is_send' => 1
            ];
            $res = Db::name('jlou_order')->where('order_id', $post['data']['id'])->update($data);
            if ($res) {
                //查询 发送站内消息
                $user = Db::name('jlou_order')->alias('a')->field('a.dk_code,a.uid,b.shop_name')->join('jlou_goods b', 'a.goods_id=b.id', 'left')
                    ->where('order_id', $post['data']['id'])
                    ->find();
                send_user_mes($user['uid'], "发货通知", "您在捡漏中购买的" . $user['shop_name'] . "已发货,发货单号为" . $user['dk_code']);
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 修改快递单号
     */
    public function edit_code()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = [
                'dk_code' => $post['data']['code'],
            ];
            $res = Db::name('jlou_order')->where('order_id', $post['data']['id'])->update($data);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /********************************************中奖**********************************************/
    public function have_goods()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['order_sn']) && $pagedata['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $pagedata['order_sn'] . '%');
            }
            if (isset($pagedata['date']) && $pagedata['date'] != '') {
                $where['date'] =  $pagedata['date'];
            }

            $count = Db::name('jlou_order')->alias('a')->field('a.*,b.date')->join('jlou_goods b', 'a.goods_id=b.id', 'left')
                ->where($where)->where('is_have',1)->count();
            $data = Db::name('jlou_order')->alias('a')->field('a.*,b.date,b.is_kaijiang')->join('jlou_goods b', 'a.goods_id=b.id', 'left')
                ->where($where)
                ->where('is_have',1)
                ->limit($startrow, $pagesize)
                ->order('order_id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $res = Db::name('jlou_order')->field('order_sn')->where('is_have',1)->select();
            $this->assign('res', $res);
            $date = Db::name('jlou_goods')->field('date')->select();
            $this->assign('date', $date);
            return view();
        }
    }


    /**
     * 点击发货
     */
    public function t_send_code()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = [
                't_code' => $post['data']['content'],
                't_is_send' => 1
            ];
            $res = Db::name('jlou_order')->where('order_id', $post['data']['id'])->update($data);
            if ($res) {
                //查询 发送站内消息
                $user = Db::name('jlou_order')->alias('a')->field('a.t_code,a.uid,b.shop_name,t_shop_name')->join('jlou_goods b', 'a.goods_id=b.id', 'left')
                    ->where('order_id', $post['data']['id'])
                    ->find();
                send_user_mes($user['uid'], "发货通知", "您在捡漏获得的特殊商品" . $user['t_shop_name'] . "已发货,发货单号为" . $user['t_code']);
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 修改快递单号
     */
    public function t_edit_code()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = [
                't_code' => $post['data']['code'],
            ];
            $res = Db::name('jlou_order')->where('order_id', $post['data']['id'])->update($data);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    public function men()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];

            $count =  Db::name('jlou_order')->alias('a')->field('g.shop_name,u.phone,u.nickname,a.cancel_code')
                ->join('user u','a.uid=u.uid','left')
                ->join('jlou_goods g','a.goods_id=g.id','left')
                ->where('date',Session::get('jlou_q'))
                ->where('order_status',1)
                ->count();
            $data =  Db::name('jlou_order')->alias('a')->field('g.shop_name,u.phone,u.nickname,a.cancel_code')
                ->join('user u','a.uid=u.uid','left')
                ->join('jlou_goods g','a.goods_id=g.id','left')
                ->where('date',Session::get('jlou_q'))
                ->where('order_status',1)
                ->limit($startrow, $pagesize)
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $get = Request::instance()->get();
            Session::set('jlou_q',$get['data']);
            return view();
        }
    }

    public function cancel_code()
    {
            $array = [
                ['shop_name'=>'商品名称' ,'phone' => '手机号', 'nickname' => '昵称', 'cancel_code' => '核销码'],
            ];

            $date = Db::name('jlou_order')->alias('a')->field('g.shop_name,u.phone,u.nickname,a.cancel_code')
                ->join('user u','a.uid=u.uid','left')
                ->join('jlou_goods g','a.goods_id=g.id','left')
                ->where('date',Session::get('jlou_q'))
                ->select();

            $data = array_merge($array, $date);
            // 下载文件名,默认当前时间'YmdHis'
            if(empty($data)){
                $filename = '';
            }else{
                $filename =$date[0]['shop_name'];
            }
            // 设置列宽,一般为strlen() + 1
            // 常用列宽
            // 手机号:12
            // 日期:11
            // 日期时间:20
            $widtharray = ['A' => 10, 'B' => 12, 'C' => 10, 'D' => 10];

            // 其他设置
            // eg:合并单元格,合并的单元格其他位置需填充''
            $settings = [
                //'mergeCells' => ['B2:B4', 'D2:D4'],
            ];

            PHPExcel::download($data, $filename, $widtharray, $settings);


    }

    /*********************************************规则**********************************************/
    public function task()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $count = Db::name('jlou_rule')->count();
            $data = Db::name('jlou_rule')
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $res = Db::name('jlou_rule')->select();
            $this->assign('res', $res);
            return view();
        }
    }

    public function addRule()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = [
                'content' => $post['data']['content'],
            ];
            $res = Db::name('jlou_rule')->insert($data);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    public function editTask()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            $data = [
                'content'=>$post['content']
            ];
            $res = Db::name('jlou_rule')->where('id',$post['id'])->update($data);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 0];
            }
        }else{
            $id = $_GET['id'];
            //查询数据库
            $data = Db::name('jlou_rule')
                ->where('id', $id)
                ->find();
            if ($data !== false) {
                $this->assign('data', $data);
            }
            return $this->fetch();
        }
    }
    /*
     * 退款列表
     */
    public function refund()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['order_sn']) && $pagedata['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $pagedata['order_sn'] . '%');
            }
            $count = Db::name('jlou_refund')->count();
            $data = Db::name('jlou_refund')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $res = Db::name('jlou_refund')->field('order_sn')->select();
            $this->assign('res',$res);
            return view();
        }
    }

    /*
     * excel
     */
    public function excel()
    {
        $array = [
            ['name' => '收货人姓名', 'address' => '收货人地址', 'phone' => '收货人电话'],
        ];

        $data = Session::get('jlou_data');
        $excel = [];
        foreach($data as $k=>$v){
           $excel[] = [
               'name'=>$v['name'],
               'address'=>$v['address'],
               'phone'=>$v['phone'],
           ];

        }
        $data = array_merge($array, $excel);
        // 下载文件名,默认当前时间'YmdHis'
        if(empty(Session::get('jlou_data'))){
            $filename = '';
        }else{
            $filename = Session::get('jlou_data')[0]['date'].'期捡漏';
        }
        Session::set('jlou_data',null);
        // 设置列宽,一般为strlen() + 1
        // 常用列宽
        // 手机号:12
        // 日期:11
        // 日期时间:20
        $widtharray = ['A' => 10, 'B' => 10, 'C' => 10, 'D' => 10];

        // 其他设置
        // eg:合并单元格,合并的单元格其他位置需填充''
        $settings = [
            //'mergeCells' => ['B2:B4', 'D2:D4'],
        ];

        PHPExcel::download($data, $filename, $widtharray, $settings);
    }
}